(function() {
	var apiManager = function () {
		this.api = 'http://www.findlark.com/extends/webMetal/api.php';
		
		this.allowKeys = {paperSilver:true, paperGold: true}; // silver|gold|silver_port|gold_port
		this.dataKey = '';
		
		this.eleLength = 6;
		
		this.zoom = {paperSilver:1000, paperGold: 100};
		
		this.keys = {timeline: 1, rate: 2, silver: 3, paperSilver: 4, gold:5, paperGold: 6};
		
		this.data = {
			// 价格数据
			price:[],
			priceSmall:[],
			priceHistory:{},
			priceMax: 0,
			priceMin: 0,
			pricePoor: 0,
			
			// 时间数据
			timeline:[],
			timelineSmall:[],
			
			rate: 0, //当前汇率
			gold: 0, // 当前现货黄金价格
			silver: 0, // 当前现货黄金价格
			paperGold: 0, // 当前纸黄金价格
			paperSilver.: 0, // 当前纸白银价格
		};
	}
	
	/**
	 * 初始化
	 */
	apiManager.prototype.init = function() {
		this.dataKey = this.getKey();
		if(!this.allowKeys[this.dataKey]) this.dataKey = 'paperSilver';
		
	}
	
	// 获取 类型
	apiManager.prototype.getKey = function() {
		
		return 'paperSilver';
	}
	
	/**
	 * 获取 当前数据
	 */
	apiManager.prototype.getCurrent = function(callback) {
		var _this = this;
		
		$.get(this.api, function(line) {
			_this.setPriceData(line, true);
			
			if(typeof callback == 'function') callback();
		});
	}
	
	/**
	 * 获取今天的数据
	 */
	apiManager.prototype.getToday = function(callback) {
		var _this = this;
		
		$.get(this.api, {day: 1}, function(text) {
			var lines = text.split("\n");
			
			for(var i = 0, l = lines.length; i< l; i++) {
				_this.setPriceData(lines[i]);
			}
			
			_this.setPriceLimit();
			if(typeof callback == 'function') callback();
		});
	}
	
	/**
	 * 获取 历史数据
	 */
	apiManager.prototype.getHistory = function(day, callback) {
		
		
	}
	
	/**
	 * 记录价格
	 * @params line 数据行
	 * @params isUpdate 是否为 后续更新
	 */
	apiManager.prototype.setPriceData = function(line, isUpdate) {
		var temp = line.split("|");
		if(temp.length < this.eleLength) return false;
		
		var k = this.keys[this.dataKey],
			timeline = temp[this.keys.timeline],
			price = temp[k] * this.zoom[this.dataKey];
		
		if(timeline % 70 == 0) {
			this.data.priceSmall.push(price);
			this.data.timelineSmall.push(timeline);
		}
		
		this.data.price.push(price);
		this.data.timeline.push(timeline);
		
		// 是否为 数据更新
		if(isUpdate) {
			this.data.rate = temp[this.keys.rate];
			this.data.gold = temp[this.keys.gold];
			this.data.paperGold = temp[this.keys.paperGold];
			this.data.silver = temp[this.keys.silver];
			this.data.paperSilver = temp[this.keys.paperSilver];
			
			this.data.priceMax = Math.max(price, this.data.priceMax);
			this.data.priceMin = Math.min(price, this.data.priceMin);
			
			this.data.pricePoor = this.data.priceMax - this.data.priceMin;
		}
	}
	
	/**
	 * 计算 价格的 上下限，差值
	 */
	apiManager.prototype.setPriceLimit = function() {
		this.data.priceMax = Math.max.apply(null, this.data.price);
		this.data.priceMin = Math.min.apply(null, this.data.price);
		
		this.data.pricePoor = this.data.priceMax - this.data.min;
	}
	
	apiManager.prototype.parseLine = function(line) {
		return line.split("|");
	}
	
	if(!window.API) {
		window.API = new apiManager;
		API.init();
	}
})();